Page({
    data: {
        showBeforePwd: false, //旧密码是否可见
        pwdBeforeType: 'password',
        showNewPwd: false, //新密码是否可见
        pwdNewType: 'password',
        showNewPwd1: false, //确认的新密码是否可见
        pwdNewType1: 'password',
        oldPwdText: '',
        newPwdText: '',
        newPwdText1: '',
        btnLoading: false,
    },

    onLoad(options) {},

    onTapBeforePwdicon() {
        if (this.data.showBeforePwd) {
            this.setData({
                pwdBeforeType: 'password',
            });
        } else {
            this.setData({
                pwdBeforeType: 'text',
            });
        }
        this.setData({
            showBeforePwd: !this.data.showBeforePwd,
        });
    },

    onTapNewPwdicon() {
        if (this.data.showNewPwd) {
            this.setData({
                pwdNewType: 'password',
            });
        } else {
            this.setData({
                pwdNewType: 'text',
            });
        }
        this.setData({
            showNewPwd: !this.data.showNewPwd,
        });
    },
    onTapPwdicon1() {
        if (this.data.showNewPwd1) {
            this.setData({
                pwdNewType1: 'password',
            });
        } else {
            this.setData({
                pwdNewType1: 'text',
            });
        }
        this.setData({
            showNewPwd1: !this.data.showNewPwd1,
        });
    },

    onoldPwdText(e) {
        this.setData({
            oldPwdText: e.detail.value,
        });
    },

    onnewPwdText(e) {
        this.setData({
            newPwdText: e.detail.value,
        });
    },

    onnewPwdText1(e) {
        this.setData({
            newPwdText1: e.detail.value,
        });
    },

    submit() {
        if (!this.data.oldPwdText)
            return wx.showToast({
                title: '请输入原密码',
                icon: 'none',
            });

        if (!this.data.newPwdText)
            return wx.showToast({
                title: '请输入新密码',
                icon: 'none',
            });

        if (!this.data.newPwdText1)
            return wx.showToast({
                title: '请再次输入新密码',
                icon: 'none',
            });

        if (this.data.newPwdText != this.data.newPwdText1)
            return wx.showToast({
                title: '两次密码不一致',
                icon: 'none',
            });
        this.setData({
            btnLoading: true,
        });

        this.request({
            url: 'user/my/safety/updatePassword',
            method: 'POST',
            data: {
                type: 1,
                old_password: this.data.oldPwdText,
                password: this.data.newPwdText,
            },
        })
            .then(async res => {
                this.setData({
                    btnLoading: false,
                });
                wx.showToast({
                    title: res.msg,
                });
                await this.sleep(1000);
                wx.switchTab({
                    url: '/pages/user/user',
                });
            })
            .catch(err => {
                this.setData({
                    btnLoading: false,
                });
            });
    },
});
